<template>
  <el-form :model="formData" label-width="80px">
    <el-form-item label="云目录:">
      <el-select v-model="formData.bucketId" size="mini">
        <el-option v-for="(bucket,index) in bucketList" :key="index" :value="bucket.id" :label="bucket.title"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="目录名:">
      <el-input size="mini" v-model="formData.name" placeholder="目录名称"></el-input>
    </el-form-item>
    <el-form-item label="目录描述">
      <el-input size="mini" type="textarea" v-model="formData.description" placeholder="目录说明"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即创建</el-button>
      <el-button @click="onCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import {mapGetters, mapActions} from 'vuex'
  export default {
    name: "new-folder",
    props:['parent'],
    data(){
      return {
        formData:{
          bucketId:null,
          name:null,
          description:null,
          parentId:0
        },
        bucketList:null
      }
    },
    computed:{
      ...mapGetters(['department']),
    },
    created(){
      //加载 所有 cos 桶
      this.loadBucketList()
    },
    methods:{
      ...mapActions({
        fetch: 'fetch'
      }),
      loadBucketList(){
        this.fetch({name:'getCosBucketList'}).then(res=>{
          console.log(res)
          this.bucketList = res
        }).catch(err=>{
          console.log(err)
        })
      },
      onSubmit(e){
        // 检查下数据
        if(!this.formData.bucketId){
          this.$message.error('云目录不能为空')
          return
        }
        if(!this.formData.name || this.formData.name === ''){
          this.$message.error('目录名称不能为空')
          return
        }
        // companyId
        this.formData.companyId = this.department.companyId
        // parentId
        if(this.parent)
          this.formData.parentId = this.parent.id

        this.fetch({name:'createDir',data:this.formData}).then(res=>{
          this.$emit('success',res)
        }).catch(err=>{
            this.$message.error(err.errMsg? err.errMsg:err)
        })
      },
      onCancel(e){
        this.$emit('cancel')
      }
    }
  }
</script>

<style lang="scss">

</style>
